package seatiger.tomcat.catalina.core;

import seatiger.tomcat.catalina.*;
import seatiger.tomcat.util.StringManager;
import seatiger.tomcat.util.http.mapper.MapperRegistry;

import java.io.IOException;
import java.net.URL;

/**
 * Host 实现
 */
public class StandardHost extends ContainerBase implements Host, Deployer {

    private String appBase;

    private Deployer deployer = new StandardHostDeployer(this);

    public StandardHost(){
        pipeline.setBasic(new StandardHostValve());
    }

    protected  static  StringManager sm = StringManager.getManager(Constants.PACKAGE_CORE);

    private String mapperClass =
            "seatiger.tomcat.catalina.core.StandardHostMapper";

    /**
     * 部署应用的 默认Context实现
     */
    private String contextClass = "seatiger.tomcat.catalina.core.StandardContext";

    /**
     * 默认的监听器配置
     */
    private String configClass =
            "seatiger.tomcat.catalina.startup.ContextConfig";

    /**
     * 自动部署的标志
     */
    private boolean autoDeploy = true;



    /**
     * 是否使用xml部署
     */
    private boolean deployXML = true;
    /**
     * 是否动态部署
     */
    private boolean liveDeploy = true;

    public String getConfigClass() {
        return configClass;
    }

    public void setConfigClass(String configClass) {
        this.configClass = configClass;
    }

    public String getContextClass() {
        return contextClass;
    }

    public void setContextClass(String contextClass) {
        this.contextClass = contextClass;
    }

    @Override
    public Context map(String uri) {
        if (uri == null){
            return null;
        }
        Context context = null;
        String mapuri = uri;

        while ((context = (Context) findChild(mapuri)) == null){
            int slash = mapuri.lastIndexOf('/');
            if (slash < 0){
                break;
            }
            mapuri = mapuri.substring(0,slash);
        }
        return context;
    }

    @Override
    public void addChild(Container child) {
        if (!(child instanceof Context)){
            throw new IllegalArgumentException(sm.getString("standardHost.notContext"));
        }
        super.addChild(child);
    }

    @Override
    public String getAppBase() {
        return this.appBase;
    }

    @Override
    public void setAppBase(String appBase) {
        this.appBase = appBase;
    }

    @Override
    protected void addDefaultMapper(String mapperClass) {
        super.addDefaultMapper(this.mapperClass);
    }

    @Override
    public void install(URL config) throws IOException {
        deployer.install(config);
    }

    @Override
    public void install(String contextPath, URL dir) throws IOException{
        deployer.install(contextPath,dir);
    }

    @Override
    public String[] findDeployedApps() {
        return deployer.findDeployedApps();
    }

    @Override
    public Context findDeployedApp(String contextPath) {
        return deployer.findDeployedApp(contextPath);
    }

    @Override
    public void setAutoDeploy(boolean autoDeploy) {
        this.autoDeploy = autoDeploy;
    }

    @Override
    public boolean getAutoDeploy() {
        return this.autoDeploy;
    }


    public boolean isDeployXML() {
        return deployXML;
    }

    public void setDeployXML(boolean deployXML) {
        this.deployXML = deployXML;
    }

    public boolean isLiveDeploy() {
        return liveDeploy;
    }

    public void setLiveDeploy(boolean liveDeploy) {
        this.liveDeploy = liveDeploy;
    }

    @Override
    public void remove(String contextPath) throws IOException {
        deployer.remove(contextPath);
    }

    @Override
    public void init() {
        MapperRegistry.registryHost(getName(),this);
        initialized = true;
    }
}
